package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.ZParams;

import java.util.HashMap;
import java.util.Map;

public class RedisDemoZset {
    // zadd 和 zcard
    public static void test01(Jedis jedis) {
        jedis.flushAll();
        jedis.zadd("key1", 10.0, "zhangsan");
        jedis.zadd("key1", 20.0, "lisi");
        jedis.zadd("key1", 30.0, "wangwu");
        System.out.println("zcard: " + jedis.zcard("key1"));
        System.out.println("zrange: " + jedis.zrange("key1", 0, -1));
    }

    // zcount zrange...
    public static void test02(Jedis jedis) {
        jedis.zadd("key1", 90.0, "zhaoliu");
        jedis.zadd("key1", 80.0, "aaa");
        System.out.println("zcount: " + jedis.zcount("key1", 20, 80));
        System.out.println("zrange: " + jedis.zrange("key1", 2, 4));
        System.out.println("zrevrange: " + jedis.zrevrange("key1", 2, 4));
        System.out.println("zrangebyscore: " + jedis.zrangeByScore("key1", 20, 40));
    }

    // zpopmin 和 zpopmax
    public static void test03(Jedis jedis) {
        System.out.println(jedis.zpopmax("key1"));
        System.out.println(jedis.zpopmin("key1"));
    }

    // zrank 和 zrevrank
    public static void test04(Jedis jedis) {
        jedis.flushAll();
        Map<String, Double> hash = new HashMap<>();
        hash.put("aa", 10.1);
        hash.put("bb", 20.3);
        hash.put("cc", 69.1);
        hash.put("dd", 50.1);
        jedis.zadd("key1", hash);
        System.out.println("zrank: " + jedis.zrank("key1", "bb"));
        System.out.println("zrevrank: " + jedis.zrevrank("key1", "bb"));
    }

    // zscore 和 zrem..
    public static void test05(Jedis jedis) {
        System.out.println(jedis.zscore("key1", "aa"));
        System.out.println(jedis.zrem("key1", "aa", "cc", "ee"));
        System.out.println(jedis.zrange("key1", 0, -1));
    }

    // 集合间的操作
    public static void test06(Jedis jedis) {
        jedis.flushAll();
        Map<String, Double> hash1 = new HashMap<>();
        hash1.put("aa", 10.2);
        hash1.put("bb", 30.0);
        hash1.put("cc", 50.3);
        hash1.put("dd", 80.3);
        Map<String, Double> hash2 = new HashMap<>();
        hash2.put("aa", 90.1);
        hash2.put("cc", 60.3);
        hash2.put("zz", 40.2);
        jedis.zadd("key1", hash1);
        jedis.zadd("key2", hash2);
        System.out.println("zinter: " + jedis.zinterstore("key3", new ZParams().weights(0.1, 0.4).aggregate(ZParams.Aggregate.MAX), "key2", "key1"));
        System.out.println("zunion: " + jedis.zunionstore("key4", new ZParams().weights(0.1, 0.4).aggregate(ZParams.Aggregate.MIN), "key1", "key2"));
        System.out.println("key3: " + jedis.zrangeWithScores("key3", 0, -1));
        System.out.println("key4: " + jedis.zrangeWithScores("key4", 0, -1));
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool(Constant.IP);
        try (Jedis jedis = jedisPool.getResource()){
//            test01(jedis);
//            test02(jedis);
//            test03(jedis);
//            test04(jedis);
//            test05(jedis);
            test06(jedis);
        }
    }
}
